package org.example.day08;

/**
 * 第三章节3.1.15.2: 方法wait(long)的基本使用 时间内被其他线程唤醒
 *      到时间后，如果没有被唤醒，则自动唤醒
 *      注意：wait和notify应该对应同一把锁.
 */
public class MyThread2 {
    static private Object lock = new Object();
    static private Runnable runnable1 = new Runnable() {
        @Override
        public void run() {
            try {
                synchronized (lock){
                    System.out.println("wait begin time = " + System.currentTimeMillis());
                    //放弃3s后被另一个线程唤醒。
                    lock.wait(5000);
                    System.out.println("wait end time = " + System.currentTimeMillis());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    static private Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            synchronized (lock){
                System.out.println("notify begin time = " + System.currentTimeMillis());
                lock.notify();
                System.out.println("notify end time = " + System.currentTimeMillis());
            }
        }
    };

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(runnable1);
        t1.start();
        Thread.sleep(3000);
        Thread t2 = new Thread(runnable2);
        t2.start();
    }
}
